package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Meal;
import com.itheima.service.MealService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.junit.Test;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/meal")
public class MealController {
    @Reference
    private MealService mealService;
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            PageResult meal=mealService.findPage(queryPageBean);
            return Result.success(MessageConstant.GET_MEAL_SUCCESS,meal);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.GET_MEAL_SUCCESS);
        }


    }
    @RequestMapping("/save")
    public Result addMeal(@RequestBody Meal meal){

        try {
            String sending = Sending();
            meal.setCoding(sending);
            mealService.add(meal);
            return Result.success(MessageConstant.ADD_MEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.ADD_MEAL_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result deleteMeal(Integer mealId){
        try {
            mealService.delete(mealId);
            return Result.success(MessageConstant.DELETE_MEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.DELETE_MEAL_FAIL);
        }
    }
    @RequestMapping("/findById")
    public Result findById(Integer mealId){

        try {
           Meal meal=mealService.findById(mealId);
            return Result.success(MessageConstant.QUERY_MEAL_SUCCESS,meal);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.QUERY_MEAL_FAIL);
        }
    }
    @RequestMapping("/update")
    public Result findById(@RequestBody Meal meal){

        try {
            mealService.update(meal);
            return Result.success(MessageConstant.EDIT_MEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.EDIT_MEAL_FAIL);
        }
    }


//    随机生成编码
    public String Sending(){
        String s1 = DateFormatUtils.format(new Date(), "yyyyMMddHH");
        Random random = new Random();
        String s = String.valueOf(random.nextInt(9999 - 1000 + 1) + 100);
        String mealMid="HM" + s1;

            return mealMid;
    }
}
